﻿using _1._6._2015_EntityFramework.Data;
using _1._6._2015_EntityFramework.Data.Models;
using _1._6._2015_EntityFramework.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace _1._6._2015_EntityFramework.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            //Deferred Execution
            //string sqlToExecute = "";

            //Order firstOne = null;
            
            //using (OrdersDbContext db = new OrdersDbContext())
            //{
            //    sqlToExecute = "SELECT * FROM Orders";
            //    IQueryable<Order> q = db.Orders.Include(o => o.User).AsQueryable();

            //    switch (DateTime.Now.Month)
            //    {
            //        case 1:
            //            sqlToExecute = "SELECT * FROM Orders WHERE Total > 50";
            //            q = q.Where(o => o.Total > 50);
            //            break;
            //        default:
            //            sqlToExecute = "SELECT * FROM Orders WHERE OrderDate < GETDATE()";
            //            q = q.Where(o => o.OrderDate < DateTime.Today);
            //            break;
            //    }

            //    //Press "Execute" in SQL Management Studio (We deferred execution until RIGHT NOW)
            //    List<Order> filteredOrderList = q.ToList();

            //    firstOne = filteredOrderList.FirstOrDefault();
                
            //    //Lazy LoadingUser
            //    //string firstName = firstOne.User.FirstName;
            //}
            
            //string firstName2 = firstOne.User.FirstName;
            
            #region Mock objects
            //List<Order> orders = new List<Order>();

            //orders.Add(new Order
            //{
            //    OrderDate = DateTime.Now,
            //    Total = 33.33m
            //});

            //orders.Add(new Order
            //{
            //    OrderDate = DateTime.Now.AddDays(3),
            //    Total = 4.56m
            //});

            //orders.Add(new Order
            //{
            //    OrderDate = DateTime.Now.AddHours(4),
            //    Total = 98.00m
            //});

            //orders.Add(new Order
            //{   
            //    OrderDate = DateTime.Now.AddHours(-6),
            //    Total = 652.87m
            //});

            //orders.Add(new Order
            //{
            //    OrderDate = DateTime.Now.AddMonths(1),
            //    Total = 47.85m
            //});

            #endregion

            //Filter a list of orders by total > 50.00
            //sqlToExecute = "SELECT * FROM Orders WHERE Total > 50 ORDER BY OrderDate DESC";
            //IOrderedQueryable<Order> ordersGreaterThanFifty = orders
            //    .Where(o => o.Total > 50)
            //    .OrderByDescending(o => o.OrderDate);

            ////Finally, press the "Execute" button in SQL Management Studio
            //List<Order> makeTheQueryExecute = ordersGreaterThanFifty.ToList();

            ////Same with LINQ syntax
            //List<Order> ordersGeaterThanFifty2 = (from o in orders
            //                                              where o.Total > 50
            //                                              orderby o.OrderDate descending
            //                                              select o).ToList();

            //OrdersDbContext db = new OrdersDbContext();
            IndexViewModel model = new IndexViewModel();

            using(OrdersDbContext db = new OrdersDbContext())
            {
                model.Orders = db.Orders.Select(o => new OrderViewModel
                {
                    OrderDate = o.OrderDate,
                    Total = (double)o.Total,
                    Name = o.User.FirstName + " " + o.User.LastName,
                    OrderItems = db.OrderItems.Where(oi => oi.OrderId == o.OrderId).Select(oi => new OrderItemsViewModel
                    {
                        ItemDescription = oi.Item.Description,
                        Price = oi.Price.ToString()
                    }).ToList()
                }).ToList();
            }
            

            return View(model);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Items()
        {
            OrdersDbContext db = new OrdersDbContext();
            ItemsViewModel model = new ItemsViewModel();

            model.Items = db.Items.Select(i => new ItemViewModel
            {
                Description = i.Description,
                Price = i.Price.Value,
                ItemId = i.ItemId
            }).ToList();

            return View(model);
        }

        public ActionResult EditItem(int id)
        {
            ItemViewModel model = new ItemViewModel();
            OrdersDbContext db = new OrdersDbContext();
            Item item = db.Items.FirstOrDefault(i => i.ItemId == id);

            model.Description = item.Description;
            model.Price = item.Price.Value;
            model.ItemId = item.ItemId;

            return View(model);
        }

        [ValidateAntiForgeryToken]
        [HttpPost]
        public ActionResult EditItem(ItemViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            OrdersDbContext db = new OrdersDbContext();

            Item item = db.Items.FirstOrDefault(i => i.ItemId == model.ItemId);
            item.Description = model.Description;
            item.Price = model.Price;

            db.SaveChanges();

            return RedirectToAction("Items");
        }

        public ActionResult CreateItem()
        {
            CreateItemViewModel model = new CreateItemViewModel();
            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateItem(CreateItemViewModel model)
        {
            //Check for validation errors
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            //Save the item
            OrdersDbContext db = new OrdersDbContext();

            Item newItem = new Item
            {
                Description = model.Description,
                Price = model.Price
            };

            db.Items.Add(newItem);
            db.SaveChanges();

            return RedirectToAction("Index");
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}